<?php
class NICED_MySQLiResult extends NICED_DBResult
{

	protected $db;
	protected $rst;
	protected $sql;
	protected $errorMessage;
	protected $errorCode;
	protected $rowCallbacks = array();

	public function __construct($db, $rst, $sql) 
	{
		parent::__construct($db, $rst, $sql);

		if (!$this->isSuccess()) 
		{
			$this->errorMessage = $this->db->error;
			$this->errorCode    = $this->db->errno;
		}
	}

	public function fetchRow($type = null) 
	{
		switch ($type) 
		{
			case self::NUM : 
			{
				$row = $this->rst->fetch_row();
				break;
			}
			case self::BOTH : 
			{
				$row = $this->rst->fetch_array();
			}
			case self::ASSOC : 
			{
				
			}
			default : 
			{
				$row = $this->rst->fetch_assoc();
			}
		}

        if (!$row)
        {
		    $row = array();
        }

		//call user callback
		for($i = 0; $i < sizeof($this->rowCallbacks); $i++) 
		{
			$row = call_user_func($this->rowCallbacks[$i], $row);
		}

		return $row;
	}


	public function insertId() 
	{
		if (false !== stripos($this->sql, 'insert')) 
		{
			return $this->db->insert_id;
		}

		return false;
	}

	public function isSuccess() 
	{
		 return (false == $this->rst) ? false : true;
	}

	public function free() 
	{
		$this->rst->free();
	}

}//end class
